<?php
/**
 * 通知
 * @author jiangyong<jiangyong.go@qq.com>
 * @date   2025/5/20 14:38
 */

namespace App\Http\Controllers;

use App\Models\User;
use App\Notifications\DefaultNotification;
use Illuminate\Support\Facades\Auth;
use Spatie\RouteAttributes\Attributes\Any;
use Spatie\RouteAttributes\Attributes\Middleware;
use Spatie\RouteAttributes\Attributes\Prefix;
use Illuminate\Http\Request;

#[Prefix('notify')]
class NotifyController extends Controller
{
    public $user;
    public function __construct()
    {
        //$this->middleware(['web','auth']);
        $this->middleware(function ($request, $next) {
            //$request->user = Auth::user();
            $request->user = User::find(1);
            return $next($request);
        });
    }

    /**
     * 发送通知
     * @param Request $request
     */
    #[Any('send')]
    public function send(request $request)
    {
        $user =   $request->user;
        $data = ['msg' => 'hello word', 'user' => $user];
        $opt = ['viaTypes' => [
            //'database',
            'broadcast'
        ]];
        return $user->notify(new DefaultNotification($data, $opt));
    }

    #[Any('/')]
    public function index(request $request)
    {
        $user =   $request->user;
        $notifications = $user->notifications;
        return $notifications;
    }
}
